<?php
/**
 * Application base model class.
 * Application-wide model functionality can be implemented here.
 * All application models should be inherited from this class.
 * @author Juhani Ruohotie
 */
class Core_Model implements Core_Defines
{
    /**
     * database handler
     */
    private static $db;

    /**
     * Rerturs objects datbase-handler, initializes it if needed
     * @return Core_Database
     */
    public static function getDatabaseHandler()
    {
        if (!self::$db)
        {
            self::$db = self::initDatabaseHandler();
        }
        return self::$db;
    }

    /**
     * Get database handler
     * @return Database handler
     */
    private static function initDatabaseHandler()
    {
        $db_host = Core_Registry::get('DB_HOST');
        $db_user = Core_Registry::get('DB_USER');
        $db_pass = Core_Registry::get('DB_PASS');
        $db_name = Core_Registry::get('DB_NAME');

        return Core_Database::init
            ($db_host, $db_user, $db_pass, $db_name);
    }

    /**
     * Remove reference from array
     *
     * @static
     * @param  $resultArray Reference array
     * @return array        Array without reference
     */
    private static function removeReference($resultArray)
    {
        $array = array();
        foreach($resultArray as $k => $v)
        {
            $array[$k] = $v;
        }
        return $array;
    }

    protected function getAllResults()
    {
        $db = self::getDatabaseHandler();
        $listOfResults = array();
        while ($db->fetch())
        {
            $listOfResults[] = $db->getResult();
        }
        return $listOfResults;
    }
}
